﻿using AutoMapper;
using Serilog;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using WCS_Entity.AutoQeuipment;
using WCS_Entity.Dto.AutoEquipmentDto;
using WCS_Entity.Dto.ComProtocolDto;
using WCS_Entity.Dto.RcsDto;
using WCS_Entity.Prorocol;
using WCS_Entity.Rcs;
using WCS_Repository.Interface;
using WCS_Repository.Repositories;
using WCS_Service.Interface;

namespace WCS_Service.Services
{
    public class RcsService : BaseService<RcsServiceEntity>, IRcsService
    {
        private readonly IRcsServiceRepository _rcsServiceRepository;
        private readonly IMapper _mapper;

        public RcsService(IRcsServiceRepository rcsServiceRepository,
                          IMapper mapper)
        {
            _rcsServiceRepository = rcsServiceRepository;
            _mapper = mapper;
        }
        #region 管理Rcs服务
        public async Task<RcsDto> CreateRcsAsync(CreateRcsDto dto)
        {
            RcsDto result = new RcsDto();
            var flag = await _rcsServiceRepository.ExistsAsync(r => r.rcs == dto.rcs);
            if(flag)
            {
                Log.Error($"创建RCS服务{dto.rcs}已经存在");
                return null;
            }
            var entity = _mapper.Map<RcsServiceEntity>(dto);

            entity.createTime = DateTime.Now;

            await _rcsServiceRepository.InsertAsync(entity);

            result = _mapper.Map<RcsDto>(entity);
            return result;
        }

        public async Task<bool> DeleteRcs(int id)
        {
            return await _rcsServiceRepository.DeleteByIdAsync(id);
        }

        public async Task<List<RcsDto>> QueryAllRcsAsync()
        {
            List<RcsDto> result = new List<RcsDto>();
            Expression<Func<RcsServiceEntity, object>> orderEx = p => p.createTime;
            var entityInfos = await _rcsServiceRepository.QueryPageAsync(null, orderEx, OrderByType.Desc, 1, 10);
            if (entityInfos is null || !entityInfos.Any())
            {
                Log.Error("查询所有Rcs服务失败");
                return null;
            }
            List<Task<RcsDto>> entityList = new List<Task<RcsDto>>();
            foreach (var entityInfo in entityInfos)
            {
                entityList.Add(GetDtoInfoAsync(entityInfo));
            }
            await Task.WhenAll(entityList);
            result = entityList.Select(e => e.Result).ToList();
            return result;
        }

        public async Task<RcsDto> QueryRcsAsync(int id)
        {
            RcsDto result = new RcsDto();

            var entityInfo = await _rcsServiceRepository.QueryByIdAsync(id);
            if (entityInfo is null)
            {
                Log.Error($"查询指定Rcs服务{id}失败");
                return null;
            }
            result = _mapper.Map<RcsDto>(entityInfo);
            return result;
        }
        public async Task<RcsDto> UpdateRcsAsync(UpdateRcsDto dto)
        {
            RcsDto result = new RcsDto();
            var entityInfo = await _rcsServiceRepository.QueryByIdAsync(dto.Id);
            if (entityInfo is null)
            {
                Log.Error($"编辑Rcs服务{dto.Id}失败");
                return null;
            }
            if (entityInfo.ip != dto.ip)
            { 
                entityInfo.ip = dto.ip;
            }
            if (entityInfo.port != dto.port)
            { 
                entityInfo.port = dto.port;
            }
            if (entityInfo.status != dto.status)
            { 
                entityInfo.status = dto.status;
            }
            if (entityInfo.isHttps != dto.isHttps)
            { 
                entityInfo.isHttps = dto.isHttps;
            }
            if (entityInfo.rcs != dto.rcs)
            { 
                entityInfo.rcs = dto.rcs;
            }
            if (entityInfo.serviceToken != dto.serviceToken)
            { 
                entityInfo.serviceToken = dto.serviceToken;
            }
            entityInfo.updataTime = DateTime.Now;

            await _rcsServiceRepository.UpdateAsync(entityInfo);
            result = _mapper.Map<RcsDto>(entityInfo);
            return result;
        }
        #endregion

        #region private
        private async Task<RcsDto> GetDtoInfoAsync(RcsServiceEntity entity)
        {
            return await Task.Run(() => _mapper.Map<RcsDto>(entity));
        }
        #endregion
    }
}
